﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Markup;
using System.Windows.Media;
using WQDSkin.Forms.Controls.Help;

namespace WQDSkin.Forms.Controls
{
    public class WQDSystemColors
    { 
        #region Brush

        public static SolidColorBrush DefaultBackground
        {
            get
            {
                return GetBrush("WQD.Brushes.DefaultBackground");
            }
        }

        public static SolidColorBrush DefaultForeground
        {
            get
            {
                return GetBrush("WQD.Brushes.DefaultForeground");
            }
        }

        public static SolidColorBrush ThemeBackground
        {
            get
            {
                return GetBrush("WQD.Brushes.ThemeBackground");
            }
        }

        public static SolidColorBrush ThemeForeground
        {
            get
            {
                return GetBrush("WQD.Brushes.ThemeForeground");
            }
        }

        //
        public static SolidColorBrush ThemeSuccess
        {
            get
            {
                return GetBrush("WQD.Brushes.Success");
            }
        }


        public static SolidColorBrush ThemeDanger
        {
            get
            {
                return GetBrush("WQD.Brushes.Danger");
            }
        }


        private static SolidColorBrush defaultOverBackground;
        public static SolidColorBrush DefaultOverBackground
        {
            get
            {
                if (defaultOverBackground == null)
                {
                    Color color = ChangeColor(DefaultBackground.Color, (float)-0.08);
                    defaultOverBackground = new SolidColorBrush(color);
                }

                return defaultOverBackground;
            }
        }

        private static SolidColorBrush defaultPressedBackground;
        public static SolidColorBrush DefaultPressedBackground
        {
            get
            {
                if (defaultPressedBackground == null)
                {
                    Color color = ChangeColor(DefaultBackground.Color, (float)-0.05);
                    defaultPressedBackground = new SolidColorBrush(color);
                }

                return defaultPressedBackground;
            }
        }

        #endregion

        //public static Color ThemeColor
        //{
        //    get {
        //        ResourceDictionary resource = Application.Current.Resources;
        //        Color color;
        //        if (resource.Contains("WQD.Color.ThemeColor"))
        //        {
        //            color = (Color)resource["WQD.Color.ThemeColor"];
        //        }
        //        else
        //        {
        //            color = Colors.Black;
        //        }
        //        return color;
        //    }
        //}



        #region Keys

        public static ResourceKey DefaultOverBackgroundKey
        {
            get
            {
                return CreateInstance(WQDSystemResourceKeyID.DefaultOverBackground);
            }
        }

        public static ResourceKey DefaultPressedBackgroundKey
        {
            get
            {
                return CreateInstance(WQDSystemResourceKeyID.DefaultPressedBackground);
            }
        }

        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="color"></param>
        /// <param name="correctionFactor">小于0变深</param>
        /// <returns></returns>
        public static Color ChangeColor(Color color, float correctionFactor)
        {
            // 加深：correctionFactor<0 
            // 变亮：correctionFactor > 0 
            // -1.0f <= correctionFactor <= 1.0f

            float red = (float)color.R;
            float green = (float)color.G;
            float blue = (float)color.B;

            if (correctionFactor < 0)
            {
                correctionFactor = 1 + correctionFactor;
                red *= correctionFactor;
                green *= correctionFactor;
                blue *= correctionFactor;
            }
            else
            {
                red = (255 - red) * correctionFactor + red;
                green = (255 - green) * correctionFactor + green;
                blue = (255 - blue) * correctionFactor + blue;
            }

            if (red < 0) red = 0; 
            if (red > 255) red = 255; 
            if (green < 0) green = 0; 
            if (green > 255) green = 255; 
            if (blue < 0) blue = 0; 
            if (blue > 255) blue = 255;
             
            return Color.FromArgb(color.A, (byte)red, (byte)green, (byte)blue);
        }

        private static WQDSystemResourceKey CreateInstance(WQDSystemResourceKeyID KeyId)
        {
            return new WQDSystemResourceKey(KeyId);
        }


        private static SolidColorBrush GetBrush(string key)
        {
            ResourceDictionary resource = Application.Current.Resources;
            SolidColorBrush ThemeBackground;

            if (resource.Contains(key))
            {
                ThemeBackground = (SolidColorBrush)resource[key];
            }
            else
            {
                ThemeBackground = new SolidColorBrush(Colors.Black);
            }
            return ThemeBackground;
        }

    }
}
